#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <termios.h> 
#include <unistd.h>
#include <malloc.h>

#include "interaction.h"
#include "ui_device.h"
#include "log.h"

static int gtch(char ch[]);
static int char_pixel_length(char ch[]);
static void init_message(struct message *msg, char message[],
                            int x, int y, int type, int number);
static void init_button(struct button *button, char name[],
                            int x, int y, int status, int number,
							int input_limit);
static void init_border(struct frame *frame, char message[],
                            int x, int y, int longth, int width);
static void pages_print(struct ui *ui_data, int number);
static void message_print(struct ui *ui_data, int wrong_number);
static void button_print(struct ui *ui_data);
static void border_print(struct ui *ui_data);
static void init_train_button_apply_for(struct button *button,
										char name[], int x, int y,
										int status, int nsssumber,
										int headline_amount,
										int page_turning_x,
										int page_turning_y,
										int printf_line_number,
										int space_between);
static void init_train_list_button(struct button *button,
									char name[], int x, int y,
									int serual_number,
									int information_encoding);
static int load_message(struct ui *ui_data,
						struct ui_print_ticket_data *data,
						int number);

/**
 * @brief   界面初始化
 * @param   ui_data:包含所有界面信息的指针
 */
void ui_init(struct ui *ui_data)
{
    struct ui *ui;

	/*用户登录页面初始化*/
	ui = &ui_data[USER_LOGIN];
	ui->button_num = 4;

	init_button(&ui->button[0], "账 号", 14, 37, HAVE_DATA, ID, BTN_USER_ID_SIZE);
	init_button(&ui->button[1], "密 码", 17, 37, CONCEAL_DATA, PASSWORD, BTN_USER_PASSWD_SIZE);
	init_button(&ui->button[2], "确 定", 21, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[3], "返 回", 21, 64, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 6;
	init_message(&ui->message[0], "火车售票登录系统", 9, 41, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                 "RESET_COLOR, 14, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                 "RESET_COLOR, 17, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], "你账号输入格式有误，请重新输入!", 24, 37, ERROR_MESSAGE, ID_ERROR);
	init_message(&ui->message[4], "你密码输入格式有误，请重新输入!", 24, 37, ERROR_MESSAGE, PASSWD_ERROR);
	init_message(&ui->message[5], "账户信息不匹配，请重新输入!", 24, 37, ERROR_MESSAGE, ID_NO_MATCH);

	ui->button_select = 0;
	ui->superstratun = NULL;

	/*主菜单页面初始化*/
	ui = &ui_data[MAIN_MENU];
	ui->button_num = 4;
	init_button(&ui->button[0], " 用户登录 ", 12, 43, NO_DATA, USER_LOGIN, 0);
	init_button(&ui->button[1], " 用户注册 ", 14, 43, NO_DATA, USER_REGISTER, 0);
	init_button(&ui->button[2], "管理员登录", 16, 43, NO_DATA, ADMIN_LOGIN, 0);							
	init_button(&ui->button[3], " 退  出 ", 24, 44, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 1;
	init_message(&ui->message[0], "火车售票系统", 9, 43, GENERAL_MESSAGE, 0);
	
    ui->button_select = 0;
	ui->superstratun = NULL;

	/*用户注册初始化*/
    ui = &ui_data[USER_REGISTER];
    ui->button_num = 7;

    init_button(&ui->button[0], "账 号", 12, 22, HAVE_DATA, ID, BTN_USER_ID_SIZE);
	init_button(&ui->button[1], "密 码", 12, 50, CONCEAL_DATA, PASSWORD, BTN_USER_PASSWD_SIZE);
	init_button(&ui->button[2], "姓 名", 15, 22, HAVE_DATA, NAME, BTN_NAME_SIZE);
	init_button(&ui->button[3], "身份证", 15, 50, HAVE_DATA, CITIZEN_ID, BTN_CITIZEN_ID_SIZE);
	init_button(&ui->button[4], "手机号", 18, 36, HAVE_DATA, PHONE_NUM, BTN_PHONE_NUM_SIZE);
	init_button(&ui->button[5], "确 定", 22, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[6], "返 回", 22, 64, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 13;
	init_message(&ui->message[0], "火车售票登录系统", 7, 41, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                 "RESET_COLOR, 12, 29, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                 "RESET_COLOR, 12, 57, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], UNDER_LINE":                   "RESET_COLOR, 15, 29, GENERAL_MESSAGE, 0);
	init_message(&ui->message[4], UNDER_LINE":                 "RESET_COLOR, 15, 57, GENERAL_MESSAGE, 0);
	init_message(&ui->message[5], UNDER_LINE":                 "RESET_COLOR, 18, 43, GENERAL_MESSAGE, 0);
	init_message(&ui->message[6], "你账号输入格式有误，请重新输入!", 25, 37, ERROR_MESSAGE, ID_ERROR);
	init_message(&ui->message[7], "你密码输入格式有误，请重新输入!", 25, 37, ERROR_MESSAGE, PASSWD_ERROR);
	init_message(&ui->message[8], "你姓名输入格式有误，请重新输入!", 25, 37, ERROR_MESSAGE, NAME_ERROR);
	init_message(&ui->message[9], "你身份证号输入格式有误，请重新输入!", 25, 37, ERROR_MESSAGE, CITIZEN_ID_ERROR);
	init_message(&ui->message[10], "你手机号输入格式有误，请重新输入!", 25, 37, ERROR_MESSAGE, PHONE_NUM_ERROR);
	init_message(&ui->message[11], "账户重复，请重新输入!", 25, 37, ERROR_MESSAGE, USER_REPEAT);
	init_message(&ui->message[12], "注册成功!", 25, 44, ERROR_MESSAGE, REGISTER_SUCCESS);

	ui->button_select = 0;
	ui->superstratun = NULL;

    /*用户菜单页面初始化*/
	ui = &ui_data[USER_MENU];
	ui->button_num = 4;
	init_button(&ui->button[0], " 买票 ", 12, 44, NO_DATA, BUY_TICKET_BTN, 0);
	init_button(&ui->button[1], " 退票 ", 15, 44, NO_DATA, RETURN_TICKET_BTN, 0);
	init_button(&ui->button[2], " 修改信息 ", 18, 42, NO_DATA, MODIFY_USER_BTN, 0);
	init_button(&ui->button[3], " 退出 ", 22, 44, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 1;
	init_message(&ui->message[0], "火车售票系统", 7, 42, GENERAL_MESSAGE, 0);
	
    ui->button_select = 0;
	ui->superstratun = NULL;

	/*管理员登录界面初始化*/
	ui = &ui_data[ADMIN_LOGIN];
	ui->button_num = 4;

	init_button(&ui->button[0], "账 号", 13, 37, HAVE_DATA, ADMIN_ID, BTN_ADMIN_ID_SIZE);
	init_button(&ui->button[1], "密 码", 16, 37, CONCEAL_DATA, ADMIN_PASSWORD, BTN_ADMIN_PASSWD_SIZE);
	init_button(&ui->button[2], "确 定", 21, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[3], "返 回", 21, 64, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 6;
	init_message(&ui->message[0], "火车售票登录系统", 8, 41, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                 "RESET_COLOR, 13, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                 "RESET_COLOR, 16, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], "你账号输入格式有误，请重新输入!", 24, 37, ERROR_MESSAGE, ID_ERROR);
	init_message(&ui->message[4], "你密码输入格式有误，请重新输入!", 24, 37, ERROR_MESSAGE, PASSWD_ERROR);
	init_message(&ui->message[5], "管理员信息不匹配，请重新输入!", 24, 37, ERROR_MESSAGE, NO_REPEAT);

	ui->button_select = 0;
	ui->superstratun = NULL;


	/*管理员菜单页面初始化*/
	ui = &ui_data[ADMIN_MENU];
	ui->button_num = 3;
	init_button(&ui->button[0], " 添 票 ", 12, 43, NO_DATA, ADD_TICKET_BTN, 0);
	init_button(&ui->button[1], "修改车票信息", 15, 41, NO_DATA, MODIFY_TICKET_BTN, 0);
	init_button(&ui->button[2], " 返 回 ", 21, 43, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 1;
	init_message(&ui->message[0], "火车售票系统", 7, 42, GENERAL_MESSAGE, 0);
	
    ui->button_select = 0;
	ui->superstratun = NULL;


	/*添加车票页面初始化*/
	ui = &ui_data[ADD_TICKET];
	ui->button_num = 10;

	init_train_button_apply_for(&ui->button[0], "火车票信息", 7, 44, LIST, TRAIN_LIST, 7, 16, 44, 4, 1);
	init_train_list_button(&ui->button[0], "车 次", 9, 20, TRAIN_NUMBER_MSG, 0);
	init_train_list_button(&ui->button[0], "始发站", 9, 30, ORIGIN_PLACE_MSG, 1);
	init_train_list_button(&ui->button[0], "终点站", 9, 40, DESTINATION_MSG, 2);
	init_train_list_button(&ui->button[0], "出发时间", 9, 50, DEPARTURE_TIME_MSG, 3);
	init_train_list_button(&ui->button[0], "到达时间", 9, 60, ARRIVAL_TIME_MSG, 4);
	init_train_list_button(&ui->button[0], "票价", 9, 70, PRICE_MSG, 5);
	init_train_list_button(&ui->button[0], "票量", 9, 80, TICKET_NUMBER_MSG, 6);

	init_button(&ui->button[1], "始发地", 18, 11, HAVE_DATA, ORIGIN_PLACE, BTN_ORIGIN_PLACE_SIZE);
	init_button(&ui->button[2], "目的地", 18, 40, HAVE_DATA, DESTINATION, BTN_DESTINATION_SIZE);
	init_button(&ui->button[3], "出发时间", 18, 68, HAVE_DATA, DEPARTURE_TIME, BTN_DEPARTURE_TIME_SIZE);
	init_button(&ui->button[4], "到达时间", 21, 11, HAVE_DATA, ARRIVAL_TIME, BTN_ARRIVAL_TIME_SIZE);
	init_button(&ui->button[5], "车 次", 21, 36, HAVE_DATA, TRAIN_NUM, BTN_TRAIN_NUM_SIZE);
	init_button(&ui->button[6], "价 格", 21, 60, HAVE_DATA, PRICE, BTN_PRICE_SIZE);
	init_button(&ui->button[7], "票 量", 24, 36, HAVE_DATA, TICKET_NUM, BTN_TICKET_NUM_SIZE);
	init_button(&ui->button[8], "确 定", 26, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[9], "返 回", 26, 64, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 18;
	init_message(&ui->message[0], UNDER_LINE":                    "RESET_COLOR, 18, 18, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                    "RESET_COLOR, 18, 47, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                 "RESET_COLOR, 18, 77, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], UNDER_LINE":                 "RESET_COLOR, 21, 20, GENERAL_MESSAGE, 0);
	init_message(&ui->message[4], UNDER_LINE":                 "RESET_COLOR, 21, 43, GENERAL_MESSAGE, 0);
	init_message(&ui->message[5], UNDER_LINE":                 "RESET_COLOR, 21, 67, GENERAL_MESSAGE, 0);
	init_message(&ui->message[6], UNDER_LINE":                 "RESET_COLOR, 24, 43, GENERAL_MESSAGE, 0);
	init_message(&ui->message[7], "始发地格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, ORIGIN_PLACE_NO_STRING);
	init_message(&ui->message[8], "目的地格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, DESTINATION_NO_STRING);
	init_message(&ui->message[9], "出发时间格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, DEPARTURE_TIME_FALSE);
	init_message(&ui->message[10], "到达时间格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, ARRIVAL_TIME_FALSE);
	init_message(&ui->message[11], "车次格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, TRAIN_NUM_NO_NUM);
	init_message(&ui->message[12], "票价格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, PRICE_NO_NUM);
	init_message(&ui->message[13], "票数格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, TICKET_NUM_NO_NUM);
	init_message(&ui->message[14], "车次重复，请重新输入!", 27, 37, ERROR_MESSAGE, TRAIN_NUM_REPEAT);
	init_message(&ui->message[15], "时间、地点重复，请重新输入!", 27, 37, ERROR_MESSAGE, PLACE_TIME_REPEAT);
	init_message(&ui->message[16], "添加成功!", 27, 44, ERROR_MESSAGE, ADD_SUCCESS);
	init_message(&ui->message[17], "车票信息重复!", 27, 44, ERROR_MESSAGE, ADD_FALSE);

    ui->button_select = 0;
	ui->superstratun = NULL;

	/*购买车票页面初始化*/
	ui = &ui_data[BUY_TICKET];
	ui->button_num = 4;

	init_train_button_apply_for(&ui->button[0], "火车票信息", 7, 44, LIST, TRAIN_LIST, 7, 16, 44, 4, 1);
	init_train_list_button(&ui->button[0], "车 次", 9, 20, TRAIN_NUMBER_MSG, 0);
	init_train_list_button(&ui->button[0], "始发站", 9, 30, ORIGIN_PLACE_MSG, 1);
	init_train_list_button(&ui->button[0], "终点站", 9, 40, DESTINATION_MSG, 2);
	init_train_list_button(&ui->button[0], "出发时间", 9, 50, DEPARTURE_TIME_MSG, 3);
	init_train_list_button(&ui->button[0], "到达时间", 9, 60, ARRIVAL_TIME_MSG, 4);
	init_train_list_button(&ui->button[0], "票价", 9, 70, PRICE_MSG, 5);
	init_train_list_button(&ui->button[0], "票量", 9, 80, TICKET_NUMBER_MSG, 6);
	
	init_button(&ui->button[1], "车 次", 19, 37, HAVE_DATA, TRAIN_NUM, BTN_TRAIN_NUM_SIZE);
	init_button(&ui->button[2], "确 定", 22, 33, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[3], "返 回", 22, 58, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 5;
	init_message(&ui->message[0], UNDER_LINE":                 "RESET_COLOR, 19, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], "车次格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, TRAIN_NUM_NO_NUM);
	init_message(&ui->message[2], "车票卖完了，请重新输入!", 25, 37, ERROR_MESSAGE, NO_TICKET_NUM);
	init_message(&ui->message[3], "没有该车票，请重新输入!", 25, 37, ERROR_MESSAGE, NO_TRAIN_NUM);
	init_message(&ui->message[4], "购买成功!", 25, 44, ERROR_MESSAGE, BUY_TICKET_SUCCESS);

    ui->button_select = 0;
	ui->superstratun = NULL;

	/*退票页面初始化*/
	ui = &ui_data[RETURN_TICKET];
	ui->button_num = 4;

	init_train_button_apply_for(&ui->button[0], "用户车票信息", 7, 44, LIST, TRAIN_LIST, 7, 16, 44, 4, 1);
	init_train_list_button(&ui->button[0], "车 次", 9, 20, TRAIN_NUMBER_MSG, 0);
	init_train_list_button(&ui->button[0], "始发站", 9, 30, ORIGIN_PLACE_MSG, 1);
	init_train_list_button(&ui->button[0], "终点站", 9, 40, DESTINATION_MSG, 2);
	init_train_list_button(&ui->button[0], "出发时间", 9, 50, DEPARTURE_TIME_MSG, 3);
	init_train_list_button(&ui->button[0], "到达时间", 9, 60, ARRIVAL_TIME_MSG, 4);
	init_train_list_button(&ui->button[0], "票价", 9, 70, PRICE_MSG, 5);
	init_train_list_button(&ui->button[0], "票量", 9, 80, TICKET_NUMBER_MSG, 6);

	init_button(&ui->button[1], "车 次", 19, 37, HAVE_DATA, TRAIN_NUM, BTN_TRAIN_NUM_SIZE);
	init_button(&ui->button[2], "确 定", 22, 33, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[3], "返 回", 22, 58, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 4;
	init_message(&ui->message[0], UNDER_LINE":                 "RESET_COLOR, 19, 44, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], "车次格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, TRAIN_NUM_NO_NUM);
	init_message(&ui->message[2], "没有该车票，请重新输入!", 25, 37, ERROR_MESSAGE, NO_TRAIN_NUM);
	init_message(&ui->message[3], "退票成功!", 25, 44, ERROR_MESSAGE, RETURN_TICKET_SUCCESS);

    ui->button_select = 0;
	ui->superstratun = NULL;


	/* 修改用户信息 */
	ui = &ui_data[MODIFY_USER];
	ui->button_num = 6;
	init_button(&ui->button[0], "密 码", 12, 22, CONCEAL_DATA, PASSWORD, BTN_USER_PASSWD_SIZE);
	init_button(&ui->button[1], "姓 名", 12, 50, HAVE_DATA, NAME, BTN_NAME_SIZE);
	init_button(&ui->button[2], "手机号", 16, 22, HAVE_DATA, PHONE_NUM, BTN_PHONE_NUM_SIZE);
	init_button(&ui->button[3], "身份证号", 16, 50, HAVE_DATA, CITIZEN_ID, BTN_CITIZEN_ID_SIZE);
	init_button(&ui->button[4], "确 定", 21, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[5], "返 回", 21, 60, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 11;
	init_message(&ui->message[0], "火车售票系统", 7, 42, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                 "RESET_COLOR, 12, 29, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                   "RESET_COLOR, 12, 57, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], UNDER_LINE":                 "RESET_COLOR, 16, 29, GENERAL_MESSAGE, 0);
	init_message(&ui->message[4], UNDER_LINE":                 "RESET_COLOR, 16, 59, GENERAL_MESSAGE, 0);
	init_message(&ui->message[5], "密码格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, PASSWD_ERROR);
	init_message(&ui->message[6], "姓名格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, NAME_ERROR);
	init_message(&ui->message[7], "手机号格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, PHONE_NUM_ERROR);
	init_message(&ui->message[8], "身份证号格式错误，请重新输入!", 25, 37, ERROR_MESSAGE, CITIZEN_ID_ERROR);
	init_message(&ui->message[9], "用户信息重复，请重新输入!", 25, 37, ERROR_MESSAGE, USER_REPEAT);
	init_message(&ui->message[10], "修改成功!", 25, 44, ERROR_MESSAGE, MODIFY_SUCCESS);
	
	ui->button_select = 0;
	ui->superstratun = NULL;


	/* 修改车票信息 */
	ui = &ui_data[MODIFY_TICKET];
	ui->button_num = 11;
	init_train_button_apply_for(&ui->button[0], "火车票信息", 7, 44, LIST, TRAIN_LIST, 7, 14, 44, 2, 1);
	init_train_list_button(&ui->button[0], "车 次", 9, 20, TRAIN_NUMBER_MSG, 0);
	init_train_list_button(&ui->button[0], "始发站", 9, 30, ORIGIN_PLACE_MSG, 1);
	init_train_list_button(&ui->button[0], "终点站", 9, 40, DESTINATION_MSG, 2);
	init_train_list_button(&ui->button[0], "出发时间", 9, 50, DEPARTURE_TIME_MSG, 3);
	init_train_list_button(&ui->button[0], "到达时间", 9, 60, ARRIVAL_TIME_MSG, 4);
	init_train_list_button(&ui->button[0], "票价", 9, 70, PRICE_MSG, 5);
	init_train_list_button(&ui->button[0], "票量", 9, 80, TICKET_NUMBER_MSG, 6);

	init_button(&ui->button[1], "想要修改的车次", 16, 36, HAVE_DATA, CHANGE_TRAIN_NUM, BTN_TRAIN_NUM_SIZE);
	init_button(&ui->button[2], "车 次", 19, 11, HAVE_DATA, TRAIN_NUM, BTN_TRAIN_NUM_SIZE);
	init_button(&ui->button[3], "始发站", 19, 36, HAVE_DATA, ORIGIN_PLACE, BTN_ORIGIN_PLACE_SIZE);
	init_button(&ui->button[4], "终点站", 19, 60, HAVE_DATA, DESTINATION, BTN_DESTINATION_SIZE);
	init_button(&ui->button[5], "出发时间", 22, 11, HAVE_DATA, DEPARTURE_TIME, BTN_DEPARTURE_TIME_SIZE);
	init_button(&ui->button[6], "到达时间", 22, 36, HAVE_DATA, ARRIVAL_TIME, BTN_ARRIVAL_TIME_SIZE);
	init_button(&ui->button[7], "票 量", 22, 60, HAVE_DATA, TICKET_NUM, BTN_TICKET_NUM_SIZE);
	init_button(&ui->button[8], "票 价", 24, 36, HAVE_DATA, PRICE, BTN_PRICE_SIZE);
	init_button(&ui->button[9], "确 定", 26, 32, NO_DATA, AFFIRM, 0);
	init_button(&ui->button[10], "返 回", 26, 64, NO_DATA, RETURN, 0);

	ui->frame_num = 2;
	init_border(&ui->rim[0], "$%*", 1, 1, 30, 100);
	init_border(&ui->rim[1], "$~#", 4, 6, 24, 90);

	ui->message_num = 18;
	init_message(&ui->message[0], UNDER_LINE":                 "RESET_COLOR, 16, 52, GENERAL_MESSAGE, 0);
	init_message(&ui->message[1], UNDER_LINE":                 "RESET_COLOR, 19, 18, GENERAL_MESSAGE, 0);
	init_message(&ui->message[2], UNDER_LINE":                 "RESET_COLOR, 19, 43, GENERAL_MESSAGE, 0);
	init_message(&ui->message[3], UNDER_LINE":                 "RESET_COLOR, 19, 67, GENERAL_MESSAGE, 0);
	init_message(&ui->message[4], UNDER_LINE":                 "RESET_COLOR, 22, 20, GENERAL_MESSAGE, 0);
	init_message(&ui->message[5], UNDER_LINE":                 "RESET_COLOR, 22, 45, GENERAL_MESSAGE, 0);
	init_message(&ui->message[6], UNDER_LINE":                 "RESET_COLOR, 22, 67, GENERAL_MESSAGE, 0);
	init_message(&ui->message[7], UNDER_LINE":                 "RESET_COLOR, 24, 43, GENERAL_MESSAGE, 0);
	init_message(&ui->message[8], "车次格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, TRAIN_NUM_NO_NUM);
	init_message(&ui->message[9], "始发站格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, ORIGIN_PLACE_NO_STRING);
	init_message(&ui->message[10], "终点站格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, DESTINATION_NO_STRING);
	init_message(&ui->message[11], "出发时间格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, DEPARTURE_TIME_FALSE);
	init_message(&ui->message[12], "到达时间格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, ARRIVAL_TIME_FALSE);
	init_message(&ui->message[13], "票量格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, TICKET_NUM_NO_NUM);
	init_message(&ui->message[14], "票价格式错误，请重新输入!", 27, 37, ERROR_MESSAGE, PRICE_NO_NUM);
	init_message(&ui->message[15], "修改成功!", 27, 44, ERROR_MESSAGE, MODIFY_SUCCESS);
	init_message(&ui->message[16], "车票信息重复!", 27, 44, ERROR_MESSAGE, MODIFY_FALSE);
	init_message(&ui->message[17], "没有该车次!", 27, 44, ERROR_MESSAGE, NO_TRAIN_NUM);

	ui->button_select = 0;
	ui->superstratun = NULL;
}

/**
 * @brief   界面控制
 * @param   ui_data:包含所有界面信息的指针
 * 			data：要打印的车票信息的指针
 * 			error_number：用于打印，以提醒用户输入错误
 * @return  ui_return:返回用户在界面中输入的信息
 *          NULL:运行错误
 */
struct input_data *menu_control(struct ui *ui_data,
								struct ui_print_ticket_data *data,
								int error_number)
{
	/*接收字符输入*/
	char ch[4] = "\0";
	int traverse;
	struct input_data *ui_return;

	ui_return = (struct input_data *)malloc(sizeof(struct input_data));
	
	/*信息获取载入链表*/
	load_message(ui_data, data, TRAIN_LIST);

	memset(ui_return->input, 0, 100);
	ui_return->input_count = 0;
	ui_return->option_type = 0;
	
	while (1) {
		if (ch[0] == UP) {
			ui_data->button_select--;
			if (ui_data->button_select < 0)
				ui_data->button_select = ui_data->button_num - 1;
		
		} else if (ch[0] == BELOW) {
			ui_data->button_select++;
			if (ui_data->button_select > ui_data->button_num - 1)
				ui_data->button_select = 0;
		} else if (ch[0] == ENT &&
					ui_data->button[ui_data->button_select].type ==
					NO_DATA) {
				/*按下ENT键且为不可输入按钮时候响应*/
				/*按钮编号存入*/
				ui_return->option_type =
				ui_data->button[ui_data->button_select].number;
				for (traverse = 0; traverse < ui_data->button_num;
						traverse++) {
					if ((ui_data->button[traverse].type == HAVE_DATA ) ||
						(ui_data->button[traverse].type == CONCEAL_DATA)) {
						/*记录有几个可输入按钮和隐藏输入按钮*/
						ui_return->input_count++;
						
						//先存个数
						ui_return->input[(int)strlen(ui_return->input)] =
							(int)strlen(ui_data->button[traverse].cache_data) + '0';
						//再存缓存区的字符串
						memcpy(&ui_return->input[(int)strlen(ui_return->input)],
								ui_data->button[traverse].cache_data, 
								(int)strlen(ui_data->button[traverse].cache_data));
					}
				}
	
			return ui_return;/*返回结构体地址*/
		} else if (ch[0] == BACK &&
					(ui_data->button[ui_data->button_select].type ==
						HAVE_DATA ||
						ui_data->button[ui_data->button_select].type ==
						CONCEAL_DATA)) {
			//删除键
			//<0者为非标准ascii一般中文有3个字符表示
			if (ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data) - 1] < 0) {
				ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data)-1] ='\0';
				ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data)-1] ='\0';
				ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data)-1] ='\0';   
			} else 
				ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data)-1] = '\0';

		} else if ((ui_data->button[ui_data->button_select].type ==
					HAVE_DATA ||
					ui_data->button[ui_data->button_select].type == 
					CONCEAL_DATA) &&
					ch[0] != ENT && ch[0] != UP &&
					ch[0] != BELOW && ch[0] !=LEFT &&
					ch[0] != RIGHT && ch[0] != BACK &&
					(int)strlen(ui_data->button[ui_data->button_select].cache_data) < ui_data->button[ui_data->button_select].input_limit) {
				
			memcpy(&ui_data->button[ui_data->button_select].cache_data[(int)strlen(ui_data->button[ui_data->button_select].cache_data)],
						ch, strlen(ch));
			
		} else if (ui_data->button[ui_data->button_select].type ==
					LIST &&
					ch[0] == LEFT &&
					ui_data->button[ui_data->button_select].list_.number_of
					> 0) {
			ui_data->button[ui_data->button_select].list_.number_of--;

		} else if (ui_data->button[ui_data->button_select].type ==
					LIST && ch[0] == RIGHT && 
					ui_data->button[ui_data->button_select].list_.number_of <
					ui_data->button[ui_data->button_select].list_.max_page ) {
			ui_data->button[ui_data->button_select].list_.number_of++;
		}

		printf("\033[2J\n");//刷新屏幕
		pages_print(ui_data, error_number);//打印页面

		if (ui_data->button[ui_data->button_select].type == HAVE_DATA ||
			ui_data->button[ui_data->button_select].type == CONCEAL_DATA) {
			printf("\033[?25h");
			MOVETO(ui_data->button[ui_data->button_select].cursor_x,
					ui_data->button[ui_data->button_select].cursor_y + 
					char_pixel_length(ui_data->button[ui_data->button_select].cache_data)
					+ 1);
		} else 
			printf("\033[?25l");/*进行光标移动或者隐藏光标*/

		ch[0] = 0;
		ch[1] = 0;
		ch[2] = 0;
		ch[3] = 0;
		gtch(ch);
	}
	
	return NULL;
}

/**
 * @brief   界面的打印信息初始化
 * @param   msg:存储的指针
 * 			message：打印的信息
 * 			x、y：位置
 * 			type：信息类型
 * 			number：以区别不同的信息
 */
static void init_message(struct message *msg, char message[],
                            int x, int y, int type, int number)
{
	memcpy(msg->message, message, strlen(message) + 1);
	msg->origin_x = x;
	msg->origin_y = y;
	msg->message_type = type;
	msg->number = number;
}

/**
 * @brief   界面的按键信息初始化
 * @param   button:存储的指针
 * 			name：按键的名字
 * 			x、y：位置
 * 			status：信息类型
 * 			number：以区别不同的信息
 * 			input_limit：输入限制的个数
 */
static void init_button(struct button *button, char name[],
                            int x, int y, int status, int number,
							int input_limit)
{
	memcpy(button->name, name, (strlen(name) + 1));
	button->place_x = x;
	button->place_y = y;
	button->type = status;
	memset(button->cache_data,0,20);
	button->number = number;
	button->cursor_x = x;
	button->cursor_y = y + char_pixel_length(name) + 2;
	button->input_limit = input_limit;
}

/**
 * @brief   界面的边框信息初始化
 * @param   frame:存储的指针
 * 			message：边框的信息
 * 			x、y：位置
 * 			longth：边框长度
 * 			width：边框宽度
 */
static void init_border(struct frame *frame, char message[],
                            int x, int y, int longth, int width)
{
	memcpy(frame->form, message, 3);
	frame->origin_x = x;
	frame->origin_y = y;
	frame->longth = longth;
	frame->width = width;
}

/**
 * @brief   界面打印车票信息的初始化
 * @param   button:存储的指针
 * 			name：按键的名字
 * 			x、y：位置
 * 			serual_number：以区别不同的信息
 * 			information_encoding：信息编码
 */
static void init_train_list_button(struct button *button, char name[],
									int x, int y, int serual_number,
									int information_encoding)
{
	if (button->type != LIST)
		return;
	
	memcpy(button->list_.name[information_encoding].name, name,
			strlen(name) + 1);
	button->list_.name[information_encoding].serial_number =
			serual_number;
	button->list_.name[information_encoding].place_x = x;
	button->list_.name[information_encoding].place_y = y;
}

/**
 * @brief   申请界面打印车票信息的初始化
 * @param   button:存储的指针
 * 			name：按键的名字
 * 			x、y：位置
 * 			status：按钮对类型
 * 			nsssumber：按钮编号
 * 			headline_amount：打印信息列数
 * 			page_turning_x、page_turning_y：翻页组件的位置
 * 			printf_line_number：打印信息行数
 * 			space_between：打印信息行间距
 */
static void init_train_button_apply_for(struct button *button,
									char name[], int x, int y,
									int status, int nsssumber,
									int headline_amount,
									int page_turning_x,
									int page_turning_y,
									int printf_line_number,
									int space_between)
{	
	memcpy(button->name, name, (strlen(name) + 1));
	button->place_x = x;
	button->place_y = y;
	button->type = status;
	button->number = nsssumber;
	button->list_.headline_amount = headline_amount;
	button->list_.page_turning_x = page_turning_x;
	button->list_.page_turning_y = page_turning_y;
	button->list_.printf_line_number = printf_line_number;
	button->list_.space_between = space_between;
	button->list_.number_of = 0;
}

/**
 * @brief   界面打印
 * @param   ui_data：界面信息的指针
 * 			number：用于打印，以提醒用户输入错误
 */
static void pages_print(struct ui *ui_data, int number)
{
	int i = 0;/*记录移动到第几个边框*/
	int x = 0, y;/*x,y记录清空位置的坐标移动*/
	int initial_site_x = 0, initial_site_y = 0,
			page_size_x = 0, page_size_y = 0; 

	if (ui_data->superstratun) {
		pages_print(ui_data->superstratun, NO_ERROR);
	}

	/*以边框的信息或许页面初始位置*/
	while (i < ui_data->frame_num) {
		if (i == 0) {
			initial_site_x = ui_data->rim[i].origin_x;
			initial_site_y = ui_data->rim[i].origin_y;
			page_size_x = ui_data->rim[i].longth;
			page_size_y = ui_data->rim[i].width;
		} else {
			if (initial_site_x > ui_data->rim[i].origin_x) {
				initial_site_x = ui_data->rim[i].origin_x;
			}
			if (initial_site_y > ui_data->rim[i].origin_y) {
				initial_site_y = ui_data->rim[i].origin_y;
			}
			if (page_size_x < ui_data->rim[i].longth) {
				page_size_x = ui_data->rim[i].longth;
			}
			if (page_size_y < ui_data->rim[i].width) {
				page_size_y = ui_data->rim[i].width;
			}
		}
		i++;
	}

	/*对页面清空*/
	for (x = initial_site_x; x < initial_site_x + page_size_x; x++) {
		for (y = initial_site_y; y < initial_site_y + page_size_y; y++) {
			printf(" ");
		}
	}

	border_print(ui_data);
	message_print(ui_data, number);
	button_print(ui_data);
}

/**
 * @brief   界面控制
 * @param   ui_data:包含所有界面信息的指针
 * 			data：要打印的车票信息的指针
 * 			number：用于打印，以提醒用户输入错误
 * @return  0:运行错误
 * 			1:运行正确
 * 			-1:不需要打印车票信息
 */
static int load_message(struct ui *ui_data,
						struct ui_print_ticket_data *data,
						int number)
{
	int record = 0;
	int i;
	int max = 0;
	struct ui_print_ticket_data *p;
	struct button *button = NULL;

	/*查找对应编号的按钮地址*/
	for (record = 0; record < ui_data->button_num; record++) {
		if (ui_data->button[record].number == number) {
			button = &ui_data->button[record];
			break;
		}
	}
	
	/*非列表按钮报错*/
	if (!button || button->type != LIST) {
		return -1;
	} else {
		p = data;
		button->list_.page_number_head[max] = p;
		/*对页数头指针进行初始化*/
		for (i = 0; p; i++) {
			if (i == 0 ) {
				button->list_.page_number_head[max] = p;
				button->list_.page_number_head[max + 1] = NULL;
			} else if (!(i % (button->list_.printf_line_number - 1))) {
				max++;
				button->list_.page_number_head[max] = p;
				button->list_.page_number_head[max + 1] = NULL;
			}
			p = p->next;
	
		}
		/*查找能最大页数*/
		button->list_.max_page = max;
		return 1;
	}

	return 0;
}

/**
 * @brief   界面信息打印
 * @param   ui_data:包含所有界面信息的指针
 * 			wrong_number：用于打印，以提醒用户输入错误
 */
static void message_print(struct ui *ui_data, int wrong_number)
{
	int record = 0;

	for (record = 0; record < ui_data->message_num; record++) {
		if (ui_data->message[record].message_type &&
            (ui_data->message[record].number == wrong_number)) {
			MOVETO(ui_data->message[record].origin_x, ui_data->message[record].origin_y);
			printf("%s\n", ui_data->message[record].message);
		} else if (!ui_data->message[record].message_type) {
			MOVETO(ui_data->message[record].origin_x, ui_data->message[record].origin_y);
			printf("%s\n", ui_data->message[record].message);
		}
	}
}

/**
 * @brief   界面按键打印
 * @param   ui_data:包含所有界面信息的指针
 */
static void button_print(struct ui *ui_data)
{
	int record = 0;
	int rank, row;
	int i;
	int message_space_between;
	struct ui_print_ticket_data *p;

	for (record = 0; record < ui_data->button_num; record++) {
		/*按钮位置判断打印*/
		if (ui_data->button_select == record &&
				ui_data->button[record].type != LIST) {
			MOVETO(ui_data->button[record].place_x,
			 		ui_data->button[record].place_y);
			printf("\033[36m>%s<\033[0m\n",
					ui_data->button[record].name);
		} else if (ui_data->button[record].type != LIST) {
			MOVETO(ui_data->button[record].place_x,
						ui_data->button[record].place_y);
			printf(" %s\n",ui_data->button[record].name);
		}
			
		if (ui_data->button[record].type == LIST) {
			message_space_between =
						ui_data->button[record].list_.space_between;
			MOVETO(ui_data->button[record].place_x,
					ui_data->button[record].place_y);
			//打印火车信息按钮名字
			printf(" %s\n", ui_data->button[record].name);

			for(rank = 0;
				rank < ui_data->button[record].list_.headline_amount;
				rank++) {
				MOVETO(ui_data->button[record].list_.name[rank].place_x,
						ui_data->button[record].list_.name[rank].place_y);

				/*打印对应列的标题*/
				printf(" %s\n", ui_data->button[record].list_.name[rank].name);

				/*获取对应页数的头指针*/
				p = ui_data->button[record].list_.page_number_head[(ui_data->button[record].list_.number_of)];
						
				for(row = 0;
					row < ui_data->button[record].list_.printf_line_number && p;
					row++) {

					//打印指针不为空且非空字符串
					if (p &&
						(p->train_message[ui_data->button[record].list_.name[rank].serial_number] != 0)) {
						
						MOVETO(ui_data->button[record].list_.name[rank].place_x +
							message_space_between * row + 2,
							ui_data->button[record].list_.name[rank].place_y);
						
						printf(" %s\n",
							p->train_message[ui_data->button[record].list_.name[rank].serial_number]);/*打印对应列的信息*/
						p = p->next;//往下一个移动

						if (p == NULL)
							break;
						}
					}
				}

				if (!ui_data->button[record].list_.page_number_head[ui_data->button[record].list_.number_of]) {//对应页数指针为空
					MOVETO(ui_data->button[record].list_.name[0].place_x +
							message_space_between,
							ui_data->button[record].list_.name[0].place_y);
					printf("未查到信息!!!\n");
				}

				/*对火车票信息进行打印，
					打印控制为headline_amount记录火车信息的最多列数，
					printf_line_number记录火车信息最大行数*/
				/*打印箭头*/
				if (ui_data->button_select == record) {
					MOVETO(ui_data->button[record].list_.page_turning_x,
							ui_data->button[record].list_.page_turning_y);
					printf("\033[36m<-- 第 %d 页 -->\033[0m\n",
						ui_data->button[record].list_.number_of + 1);
				} else {
					MOVETO(ui_data->button[record].list_.page_turning_x,
							ui_data->button[record].list_.page_turning_y);
					printf("<-- 第 %d 页 -->\n", ui_data->button[record].list_.number_of + 1);
				}
			}
			
			/*如果为输入型按钮把按钮缓冲区内容打印出来*/
			if (ui_data->button[record].type == HAVE_DATA) {
				MOVETO(ui_data->button[record].place_x, 
					ui_data->button[record].cursor_y+1);
				printf("\033[4m%s\033[0m",
					ui_data->button[record].cache_data);
			} else if (ui_data->button[record].type == CONCEAL_DATA) {
				MOVETO(ui_data->button[record].place_x, 
					ui_data->button[record].cursor_y+1);
				for (i = 0;
					i < char_pixel_length(ui_data->button[record].cache_data);
					i++)
					printf("\033[4m*\033[0m");
			}
	}
}

/**
 * @brief   界面边框打印
 * @param   ui_data:包含所有界面信息的指针
 */
void border_print(struct ui *ui_data)
{
	int record = 0, x, y;

	for (record = 0; record < ui_data->frame_num; record++) {
        /*第一行打印*/
		for(x = ui_data->rim[record].origin_x, y = ui_data->rim[record].origin_y; 
				x <= ui_data->rim[record].origin_x + ui_data->rim[record].longth; x++) {

			if(x == ui_data->rim[record].origin_x || x == ui_data->rim[record].origin_x + 
					ui_data->rim[record].longth) {
				MOVETO(x, y);
				printf("%c", ui_data->rim[record].form[0]);
			} else {
				MOVETO(x, y);
				printf("%c", ui_data->rim[record].form[1]);
			}
		}
        /*最后一行打印*/
		for(x = ui_data->rim[record].origin_x, y = ui_data->rim[record].origin_y + ui_data->rim[record].width;
				x <= ui_data->rim[record].origin_x + ui_data->rim[record].longth; x++) {
			
				if(x == ui_data->rim[record].origin_x || x == ui_data->rim[record].origin_x + 
						ui_data->rim[record].longth) {

					MOVETO(x, y);
					printf("%c", ui_data->rim[record].form[0]);

				} else {

					MOVETO(x, y);
					printf("%c", ui_data->rim[record].form[1]);
				}
			
		}
        /*打印第一列*/
		for(x = ui_data->rim[record].origin_x, y = ui_data->rim[record].origin_y; 
				y <= ui_data->rim[record].origin_y + ui_data->rim[record].width; y++) {

			if(y == ui_data->rim[record].origin_y || y == ui_data->rim[record].origin_y +
					ui_data->rim[record].width) {
				MOVETO(x, y);
				printf("%c", ui_data->rim[record].form[0]);
			} else {
				MOVETO(x, y);
				printf("%c", ui_data->rim[record].form[2]);
			}
		
		}
        /*打印最后一列*/
		for(x = ui_data->rim[record].origin_x + ui_data->rim[record].longth, y = ui_data->rim[record].origin_y; 
				y <= ui_data->rim[record].origin_y + ui_data->rim[record].width; y++) {

			if(y == ui_data->rim[record].origin_y || y == ui_data->rim[record].origin_y + 
					ui_data->rim[record].width) {
				MOVETO(x, y);
				printf("%c\n", ui_data->rim[record].form[0]);
			} else {
				MOVETO(x, y);
				printf("%c\n", ui_data->rim[record].form[2]);
			}
		}
	}
}

/**
 * @brief   模仿getch
 */
static int gtch(char ch[])
{
	char input_ch[3];
	static struct termios initial_settings, new_settings;

	tcgetattr(0,&initial_settings);
	new_settings = initial_settings;
	new_settings.c_lflag &= ~ICANON;//对输入列队进行修改,所有输入都变成字符
	//new_settings.c_lflag &= ~ECHO;//隐藏显示
	//new_settings.c_lflag &= ~ISIG;//输入不进入缓冲区
	//new_settings.c_oflag &= ~OPOST;
	new_settings.c_lflag &= ~TOSTOP;
	new_settings.c_iflag &= ~IMAXBEL;
	new_settings.c_cc[VMIN] = 1;
	new_settings.c_cc[VTIME] = 0;
	tcsetattr(0, TCSANOW, &new_settings);
	//read(0,input_ch,sizeof(input_ch));   

	input_ch[0]= getchar();
	new_settings.c_cc[VMIN] = 0;
	tcsetattr(0, TCSANOW, &new_settings);
	input_ch[1] = getchar();
	input_ch[2] = getchar();

	if (input_ch[0] == 27 && input_ch[1] == 91 && input_ch[2] == 65) {
		tcsetattr(0, TCSANOW, &initial_settings);
		ch[0] = -106;
		ch[1] = 0;
		ch[2] = 0;
		return 1;
	} else if (input_ch[0] == 27 && input_ch[1] == 91 && input_ch[2] == 66) {
		tcsetattr(0, TCSANOW, &initial_settings);
		ch[0] = -105;
		ch[1] = 0;
		ch[2] = 0;
		return 1;
	} else if (input_ch[0] == 27 && input_ch[1] == 91 && input_ch[2] == 68) {
		tcsetattr(0, TCSANOW, &initial_settings);
		ch[0] = -104;
		ch[1] = 0;
		ch[2] = 0;
		return 1;
	} else if (input_ch[0] == 27 && input_ch[1] == 91 && input_ch[2] == 67) {
		tcsetattr(0, TCSANOW, &initial_settings);
		ch[0] = -103;
		ch[1] = 0;
		ch[2] = 0;
		return 1;
	} 

	tcsetattr(0, TCSANOW, &initial_settings);
	
	memcpy(ch ,input_ch, 3);
	if(ch[0] >= 0) {
		ch[1] = 0;
		ch[2] = 0;
	}

	return 0;
}

/**
 * @brief   计算用户输入的长度
 * @param	ch：用户输入的数组
 * @return	pixel_length：数组长度
 */
static int char_pixel_length(char ch[])
{
	int i = 0;
	int char_length = 0;
	int pixel_length = 0;

	char_length = (int)strlen(ch);

	/*i<字符串长度防止越界&&ch[i]不为0*/
	while (i < char_length && ch[i]) {
		/*英文ascii码的范围为0—127*/
		if (ch[i] >= 0 && ch[i] <= 127) {
			/*英文字符像素长度为1*/
			pixel_length ++;
			i++;
		} else if (ch[i] < 0) {
			/*ascii以外的字符像素长度为2*/
			pixel_length += 2;
			/*其它字符的ascii有3位*/
			i += 3;
		}
	}

	return pixel_length;
}